var pageLoad = function(){
	objTreeInit();
	console.log($("#div_staff_list").width());
}
var objData;
//查看：fa-search-plus blue
//修改：fa-pencil-square-o red
//添加：fa-plus green
//删除：fa-trash-o orange
function editWindow(menu,success,clear){
	var html = $("#editWindow").html();
	bootbox.dialog({
		message	: html,
		buttons	:
		{
			"success" :
			 {
				"label" : "<i class='ace-icon fa fa-check'></i>提交",
				"className" : "btn-sm btn-success",
				"callback": function() {
					if(success)success();
				}
			},
			"button" :
			{
				"label" : "<i class='ace-icon fa fa-times'></i>取消",
				"className" : "btn-sm",
				"callback": function() {
					if(clear)clear();
				}
			}
		}
	});
	setFormInput(menu,"menu_form","");
}

function getObjData(id,data){
	for(var i = 0; i < data.length ; i++){
		if(data[i].organizationId == id){
			return data[i];
		}
	}
}

function removeTreeLi(id){
	$("li[data-id='"+id+"']").remove();
	$("#objTree").nestable("init");
	for(var i=0;objDatata.length;i++){
		if(id == objData[i].organizationId){
			objData.remove(i);
			break;
		}
	}
	$("#objTree").nestable("init");
}
function addTreeLi(newMenu){
	objData.push(newMenu);
	$("#objTree").html("");
	var html = readTree(objData,"-1");
	$("#objTree").append(html);
//	$("#objTree").nestable({isMove:false});
	$("#objTree").nestable("init");
}
function updateTreeLi(newMenu){
	for(var i=0;i<objData.length;i++){
		if(newMenu.organizationId == objData[i].organizationId){
			objData[i] = newMenu;
			break;
		}
	}
	$("#objTree").html("");
	var html = readTree(objData,"-1");
	$("#objTree").append(html);
	$("#objTree").nestable("init");
}

function addWindowShwo(id){
	var parentObj = getObjData(id,objData);
	var menu = {};
	if(!parentObj){
		menu.parentName = "系统根目录";
		menu.path = "-1";
		menu.parentId = "-1";
		menu.level = 1;
	}else{
		menu.parentName = parentObj.organizationName;
		menu.path = parentObj.path;
		menu.parentId = parentObj.organizationId;
		menu.level = parseInt(parentObj.level) + 1;
		
	}
	editWindow(menu,function(){
		$.ajax( {
			url : system_basePath + "sys/systemorganization/addOrganization.do",
			type:"post",
			data:$(".modal-dialog #menu_form").serialize(),
			success : function(data) {
				data = eval('(' + data + ')');
				var obj = data.obj;
				$.gritter.add({
					title: '提示',
					text: data.msg,
					class_name: 'gritter-success'
				});
				addTreeLi(obj);
				bindClick();
			}
		});
	});
}

function bindClick(){
	
	$("#objTree a[id^='menu_add_']").unbind(ace.click_event);
	$("#objTree a[id^='menu_add_']").on(ace.click_event,function(){
		var id = $(this).attr("id").split("_")[2];
		addWindowShwo(id);
	});
	
	$("#objTree a[id^='menu_edit_']").unbind(ace.click_event);
	$("#objTree a[id^='menu_edit_']").on(ace.click_event,function(){
		var id = $(this).attr("id").split("_")[2];
		var organizationObj = getObjData(id,objData);
		if(organizationObj.parentId == "-1"){
			organizationObj.parentName = "系统根目录";
			organizationObj.parentPath = "-1";
		}else{
			var parentObj = getObjData(organizationObj.parentId,objData);
			organizationObj.parentName = parentObj.organizationName;
			organizationObj.parentPath = parentObj.path;
		}
		editWindow(organizationObj,function(){
			$.ajax( {
				url : system_basePath + "sys/systemorganization/updateOrganization.do",
				type:"post",
				data:$(".modal-dialog #menu_form").serialize(),
				success : function(data) {
					data = eval('(' + data + ')');
					var obj = data.obj;
					$.gritter.add({
						title: '提示',
						text: data.msg,
						class_name: 'gritter-success'
					});
					updateTreeLi(obj)
					bindClick();
				}
			});
		});
	});
	
	
	$("#objTree a[id^='menu_del_']").unbind(ace.click_event);
	$("#objTree a[id^='menu_del_']").on(ace.click_event,function(){
		var id = $(this).attr("id").split("_")[2];
		var param = {};
		param.organizationId = id;
		var callBack = function(){
			$.ajax( {
				url : system_basePath + "sys/systemorganization/deleteOrganization.do",
				type:"post",
				data:param,
				success : function(data) {
					data = eval('(' + data + ')');
					$.gritter.add({
						title: '提示',
						text: data.msg,
						class_name: 'gritter-success'
					});
					removeTreeLi(id);
					bindClick();
				}
			});
		};
		bootbox.confirm("是否删除菜单及子菜单，删除后无法回退?", function(result) {
			if(result) {
				callBack();
			}
		});
	});
}

function selectLi(obj){
	console.log($(obj).attr("data-id"));
	$("#objTree").find(".btn-info").removeClass("btn-info");
	$(obj).children("div").addClass("btn-info");
}

function readTree(data,pid){
	var htm = "";
	for(var i = 0; i < data.length ; i++){
		var obj = data[i];
		var id = obj.organizationId;
		var parentId = obj.parentId;
		var classStyle = obj.classStyle;
		if(parentId != pid){
			continue;
		}
		
		if(!classStyle && "-1" == pid){
			classStyle = "fa-bars";
		}
		
		var organizationName = obj.organizationName;
		htm += 
			'<li class="dd-item dd2-item" data-id="'+id+'">' +
				'<div class="dd-handle dd2-handle">' +
					'<i class="normal-icon ace-icon fa '+classStyle+' bigger-150"></i>' +
				'</div>' +
				'<div class="dd2-content">' + organizationName +
					'<div class="pull-right action-buttons">' +
						'<a class="purple" href="javascript:void(0)" id="menu_add_'+id+'">' +
							'<i class="ace-icon fa bigger-150 fa-plus-circle"></i>' +
						'</a>' +
						'<a class="green" href="javascript:void(0)" id="menu_edit_'+id+'">' +
							'<i class="ace-icon fa bigger-150 fa-pencil-square-o"></i>' +
						'</a>' +
						'<a class="red" href="javascript:void(0)" id="menu_del_'+id+'">' +
							'<i class="ace-icon fa fa-trash-o bigger-150"></i>' +
						'</a>' +
					'</div>' +
				'</div>' ;
		htm += readTree(data,id);
	}
	
	if(!htm){
		return htm;
	}
	var html = '<ol class="dd-list">'+htm+'</li></ol>';
	return html;
}



function objTreeInit(){
	$.ajax({
		url : system_basePath + "sys/systemorganization/loadOrganizationList.do",
		type:"post",
		dataType:"json",
		success : function(data) {
			var html = readTree(data,"-1");
			$("#objTree").html("");
			$("#objTree").append(html);
			$("#objTree").nestable({isMove:false,
				itemClick:function(e){
					$("#objTree").find(".btn-info").removeClass("btn-info");
	            	$(e.toElement).parents("li.dd-item:first").children("div").addClass("btn-info");
				}});
			bindClick();
		}
	});
}

function setFormInput(dataMap,formId,objName){
	var getData = function(dataMap,obj){
		var name = obj.attr("name").split(".");
		name = name[name.length-1];
		var val = dataMap[name];
		return val;
	};
	var textInputs = $(".modal-dialog #" + formId).find("input:not(:button)");
	for(var i=0;i < textInputs.length ; i++){
		var obj = $(textInputs[i]);
		obj.val(getData(dataMap,obj));
	}
	
	var selects = $(".modal-dialog #" + formId).find("select");
	for(var i=0;i < selects.length ; i++){
		var obj = $(selects[i]);
		obj.val(getData(dataMap,obj));
	}
}
